package com.ssl.springframework.beans.factory.support;

import com.ssl.springframework.beans.factory.config.SignletonBeanRegistry;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 描述：
 *
 * @author ssl
 * @create 2021/6/10 0010.
 */
public class DefaultSignletonBeanRegistry implements SignletonBeanRegistry {

    // 缓存单例bean （一级缓存）
    private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);

    /**
     * 从缓存中获取单例 bean
     *
     * @param beanName
     * @return
     */
    @Override
    public Object getSingleton(String beanName) {
        return singletonObjects.get(beanName);
    }

    /**
     * 将bean添加到缓存中
     *
     * @param beanName
     * @param singletonObject
     */
    protected void addSingleton(String beanName, Object singletonObject) {
        synchronized (this.singletonObjects) {
            this.singletonObjects.put(beanName, singletonObject);
        }
    }
}
